import tkinter as tk
from tkinter import ttk
import datetime
window=tk.Tk()
window.title('To-Do List Application')
window.geometry('600x350+200+200')
def get_datedime():
    return str(datetime.datetime.now())
def save_tasks():
    tasks=listbox.get(0,tk.END)
    with open ('tasks.txt','w')as file:
        for task in tasks:
            file.write(task+'\n')
def load_tasks():
    try:
        with open('tasks.txt','r')as file:
            tasks=file.readlines()
            for task in tasks:
                listbox.insert(tk.END,task.strip())
    except FileNotFoundError:
        pass
def add_task():
    task_title=title_entry.get()
    task_priority=priority_combobox.get()
    task_date=get_datedime()
    task_description=description_entry.get()
    if task_title:
        task=f'({task_priority}){task_title}-{task_description}-{task_date}'
        listbox.insert(tk.END,task)
        title_entry.delete(0,tk.END)
        description_entry.delete(0,tk.END)
        priority_combobox.set('low')
        save_tasks()
def delete_task():
    listbox.delete(tk.ACTIVE)
    save_tasks()
input_frame=tk.Frame(window)
input_frame.pack(pady=10)    
title_label=ttk.Label(input_frame,text='title:')
title_label.pack(side=tk.LEFT,padx=10)
title_entry=ttk.Entry(input_frame,width=10)
title_entry.pack(side=tk.LEFT,padx=10)
priority_label=ttk.Label(input_frame,text='priority category:')
priority_label.pack(side=tk.LEFT,padx=10)
priority_combobox=ttk.Combobox(input_frame,width=10,values=['low','medium','high'])
priority_combobox.pack(side=tk.LEFT,padx=10)
priority_combobox.set('low')
description_label=ttk.Label(input_frame,text='description:')
description_label.pack(side=tk.LEFT,padx=10)
description_entry=ttk.Entry(input_frame,width=15)
description_entry.pack(side=tk.LEFT,padx=10)

button_frame=tk.Frame(window)
button_frame.pack(pady=10)
add_button=ttk.Button(button_frame,text='add',command=add_task)
add_button.pack(side=tk.LEFT,padx=10)
delete_button=ttk.Button(button_frame,text='delate',command=delete_task)
delete_button.pack(side=tk.LEFT,padx=10)
listbox=tk.Listbox(window,width=50)
listbox.pack()
load_tasks()
window.mainloop()
